//
//  YIem_SYTabBarView.m
//  B段_开眼
//
//  Created by YIem on 16/5/4.
//  Copyright © 2016年 Howie. All rights reserved.
//

#import "YIem_SYTabBarView.h"
#import "YIem_UIView+SYCategory.h"
#import "YIem_SYTabBarButton.h"

@interface YIem_SYTabBarView ()
@property (nonatomic, strong) YIem_SYTabBarButton *selectBtn;
@end

@implementation YIem_SYTabBarView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        // 创建六个按钮
        YIem_SYTabBarButton *firstBtn=[self setupButtonWithImageName:@"tab_bar_feed_icon" title:@"全部动态"];
        //按钮失效
        firstBtn.enabled = NO;
        //给属性按钮赋值  默认选中第一个按钮
        self.selectBtn = firstBtn;
        
        [self setupButtonWithImageName:@"tab_bar_passive_feed_icon" title:@"与我相关"];
        [self setupButtonWithImageName:@"tab_bar_pic_wall_icon" title:@"照片墙"];
        [self setupButtonWithImageName:@"tab_bar_e_album_icon" title:@"电子相框"];
        [self setupButtonWithImageName:@"tab_bar_friend_icon" title:@"好友"];
        [self setupButtonWithImageName:@"tab_bar_e_more_icon" title:@"设置"];

        
    }
    return self;
    
}
- (YIem_SYTabBarButton *) setupButtonWithImageName:(NSString *) imageName title:(NSString *)title {
    YIem_SYTabBarButton *button = [YIem_SYTabBarButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [button setTitle:title forState:UIControlStateNormal];
    button.tag = self.subviews.count;
    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchDown];
    [self addSubview:button];
    return button;
}
- (void)buttonClick:(YIem_SYTabBarButton *)btn {
    // 之前选中的按钮恢复可用
    self.selectBtn.enabled = YES;
    // 当前按钮不可用
    btn.enabled = NO;
    // 赋值  给属性
    self.selectBtn = btn;
    
    // 发送通知
    [[NSNotificationCenter defaultCenter] postNotificationName:@"SYTabBarButtonChangeNotification" object:nil userInfo:@{@"SYTabBarSelectIndex":@(btn.tag)}];
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    NSInteger count = self.subviews.count;
    for (int  i = 0 ; i <count; i++) {
        //取出SYTabBarView的子类赋给button
        YIem_SYTabBarButton *button = self.subviews[i];
        button.width = self.width;
        button.height = 70;
        button.x = 0;
        button.y = i * button.height;
        //绑定tag, 以便传值 --> 每次布局都会重复调用
        button.tag = i;
    }
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
